/* global jQuery */

jQuery(document).ready(function(){
  jQuery('.submit-form').click(function(){
    var $this = jQuery(this);
    var errore = 0;

    $this.parent().find('.required').css('border','1px solid #ccc');
    $this.parent().find('.checkbox span').removeAttr( 'style' );

    $this.parent().find('.required').each(function(){
      var $tipo = jQuery(this).attr('type');
      if( $tipo == 'email' ){
        if( !isValidEmailAddress( jQuery(this).val() ) ) {
          jQuery(this).css('border','1px solid #a94442');
          errore = 1;
        }
      }else{
        if( jQuery(this).val() == '' ){
          jQuery(this).css('border','1px solid #a94442');
          errore = 1;
        }
      }
    });

    if( !$this.parent().find('.checkbox input[name="privacy"]').is(':checked') ){
      $this.parent().find('.checkbox span').css('color', '#a94442');
      errore = 1;
    }

    if( errore == 1 ){
      $this.parent().find('.alert-danger').fadeIn();
    }else{
      $this.parent().submit();
    }
  });

  jQuery('.js-download-form').submit(function(event) {
    var $this = jQuery(this);
    var errore = 0;

    $this.parent().find('.required').css('border','1px solid #ccc');
    $this.parent().find('.checkbox span').removeAttr( 'style' );

    $this.parent().find('.required').each(function(){
      var $tipo = jQuery(this).attr('type');
      if( $tipo == 'email' ){
        if( !isValidEmailAddress( jQuery(this).val() ) ){
          jQuery(this).css('border','1px solid #a94442');
          errore = 1;
        }
      }else{
        console.log(jQuery(this).val())
        if( jQuery(this).val() == '' ){
          jQuery(this).css('border','1px solid #a94442');
          errore = 1;
        }
      }
    });

    if( !$this.parent().find('.checkbox input[name="privacy"]').is(':checked') ){
      $this.parent().find('.checkbox span').css('color', '#a94442');
      errore = 1;
    }

    if (errore === 1) {
      event.preventDefault();
      $this.parent().parent().find('.alert-danger').fadeIn();
    } else {
      $this.parent().parent().submit();
    }
  });
});

function isValidEmailAddress(emailAddress) {
  var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
  return pattern.test(emailAddress);
};
